package livesun.justiceeternal.adapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import java.util.ArrayList;
import java.util.List;

import livesun.justiceeternal.db.love.LoveBean;
import zlc.season.practicalrecyclerview.AbstractAdapter;

public class LoveAdapter extends AbstractAdapter<LoveBean,LoveViewHolder> {
    private OnItemClickListener itemClickListener;
    private boolean mMode;
    List<LoveBean> mCheckedList = new ArrayList<>();
    @Override
    protected LoveViewHolder onNewCreateViewHolder(ViewGroup parent, int viewType) {
        return new LoveViewHolder(parent);
    }

    @Override
    protected void onNewBindViewHolder(final LoveViewHolder holder, final int position) {
        holder.setDatas(getData());
        final LoveBean loveBean = get(position);
        holder.setMode(mMode);
        holder.setData(loveBean);
        holder.getCheckBox().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMode){
                    changeStatus(loveBean,holder.getCheckBox());
                    if(itemClickListener!=null){
                        itemClickListener.itemChecked(position, loveBean);
                    }
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMode){
                    changeStatus(loveBean,holder.getCheckBox());

                    if(itemClickListener!=null){
                        itemClickListener.itemChecked(position, loveBean);
                    }
                }else {
                    if(itemClickListener!=null){
                        itemClickListener.itemClick(position, loveBean);
                    }
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(itemClickListener!=null){
                    itemClickListener.onLongItemClick(position, loveBean);
                }
                setMode(true,position);
                holder.getCheckBox().setChecked(loveBean.isChecked);
                if(itemClickListener!=null){
                    itemClickListener.itemChecked(position, loveBean);
                }
                return true;
            }
        });

    }

    public void setMode(boolean edit,int position) {
        if (mMode == edit) {
            return;
        }
        if(edit){
            mCheckedList.clear();
            for (int i=0;i<getData().size();i++){
                LoveBean historyBean = getData().get(i);
                if(position==i){
                    historyBean.isChecked = true;
                    mCheckedList.add(historyBean);
                }else {
                    historyBean.isChecked = false;
                }
            }
        }else {
            for (LoveBean dat : getData()) {
                dat.isChecked = false;
            }
        }
        mMode = edit;
        notifyDataSetChanged();
    }


    public int deletItme(LoveBean note) {
        int position = getData().indexOf(note);
        if(position!=-1){
            getData().remove(position);
            mCheckedList.remove(note);
            //删除动画
            notifyItemRemoved(position);
            notifyItemRangeChanged(position,getData().size()-position);
        }

        return getData().size();
    }


    public void setChecked(LoveBean historyBean) {
        if (!mCheckedList.contains(historyBean)) {
            mCheckedList.add(historyBean);
        } else {
            mCheckedList.remove(historyBean);
        }
        notifyItemChanged(getData().indexOf(historyBean));
    }

    public void changeStatus(LoveBean item, CheckBox checkBox) {
        item.isChecked = !item.isChecked;
        checkBox.setChecked(item.isChecked );
        if(item.isChecked){
            if(!mCheckedList.contains(item)){
                mCheckedList.add(item);
            }
        }else {
            if(mCheckedList.contains(item)){
                mCheckedList.remove(item);
            }
        }
    }

    public boolean getMode(){
        return mMode;
    }

    public void checkAll() {
        mCheckedList.clear();
        for (LoveBean dat : getData()) {
            dat.isChecked = true;
        }
        mCheckedList.addAll(getData());
        notifyDataSetChanged();
    }

    public void uncheckAll() {
        for (LoveBean dat : getData()) {
            dat.isChecked = false;
        }
        mCheckedList.clear();

        notifyDataSetChanged();
    }

    public List<LoveBean> getCheckedList(){
        return mCheckedList;
    }

    public boolean isCheckAll() {
        return mCheckedList.size() == getData().size();
    }


    public interface OnItemClickListener{
        void itemClick(int position,LoveBean loveBean);
        void onLongItemClick(int position,LoveBean loveBean);
        void itemChecked(int position,LoveBean loveBean);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }


}
